package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.LaborSubcontract;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.ProfessionalSubcontracting;
import com.ruoyi.project.system.service.IProfessionalSubcontractingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 专业分包Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/system/subcontracting")
public class ProfessionalSubcontractingController extends BaseController
{
    @Autowired
    private IProfessionalSubcontractingService professionalSubcontractingService;

    /**
     * 查询专业分包列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProfessionalSubcontracting professionalSubcontracting)
    {
        startPage();
        List<ProfessionalSubcontracting> list = professionalSubcontractingService.selectProfessionalSubcontractingList(professionalSubcontracting);
        for(ProfessionalSubcontracting subcontract : list){
            subcontract.setRoleKey(SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey());
        }
        return getDataTable(list);
    }

    /**
     * 导出专业分包列表
     */
    @Log(title = "专业分包", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProfessionalSubcontracting professionalSubcontracting)
    {
        List<ProfessionalSubcontracting> list = professionalSubcontractingService.selectProfessionalSubcontractingList(professionalSubcontracting);
        ExcelUtil<ProfessionalSubcontracting> util = new ExcelUtil<ProfessionalSubcontracting>(ProfessionalSubcontracting.class);
        return util.exportExcel(list, "专业分包数据");
    }

    /**
     * 获取专业分包详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(professionalSubcontractingService.selectProfessionalSubcontractingById(id));
    }

    /**
     * 新增专业分包
     */
    @Log(title = "专业分包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProfessionalSubcontracting professionalSubcontracting)
    {
        professionalSubcontracting.setStatus("待审核");
        return toAjax(professionalSubcontractingService.insertProfessionalSubcontracting(professionalSubcontracting));
    }

    /**
     * 修改专业分包
     */
    @Log(title = "专业分包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProfessionalSubcontracting professionalSubcontracting)
    {
        return toAjax(professionalSubcontractingService.updateProfessionalSubcontracting(professionalSubcontracting));
    }

    /**
     * 删除专业分包
     */
    @Log(title = "专业分包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(professionalSubcontractingService.deleteProfessionalSubcontractingByIds(ids));
    }

    @PostMapping("/submitAgain")
    public AjaxResult submitAgain(@RequestBody ProfessionalSubcontracting professionalSubcontracting)
    {
        professionalSubcontracting.setStatus("待审核");
        professionalSubcontracting.setRemark("");
        return toAjax(professionalSubcontractingService.updateProfessionalSubcontracting(professionalSubcontracting));
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ProfessionalSubcontracting professionalSubcontracting)
    {
        professionalSubcontracting.setStatus("审核失败");
        return toAjax(professionalSubcontractingService.updateProfessionalSubcontracting(professionalSubcontracting));
    }


    @PostMapping("/agree")
    public AjaxResult agree(@RequestBody ProfessionalSubcontracting professionalSubcontracting)
    {
        professionalSubcontracting.setStatus("审核成功");
        return toAjax(professionalSubcontractingService.updateProfessionalSubcontracting(professionalSubcontracting));
    }
}
